package com.biz.primus.ms.oms.trans;

import com.biz.primus.common.utils.Copier;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.model.oms.enums.PrivilegeBehavior;
import com.biz.primus.model.oms.vo.OmsItemVo;
import com.biz.primus.ms.base.service.IdService;
import com.biz.primus.ms.oms.dao.po.OmsOrder;
import com.biz.primus.ms.oms.dao.po.OmsOrderItem;
import com.biz.primus.ms.oms.dao.po.OrderItemApportionment;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author yanweijin
 * @date 2017/11/17
 */
@Component
public class OrderItemConverter {

	@Autowired
	private IdService idService;


	public OmsItemVo po2vo(OmsOrderItem po){
		OmsItemVo vo = po2voMapping.apply(po);
		return vo;
	}

	public OmsOrderItem vo2po(OmsItemVo vo, OmsOrder order){
		OmsOrderItem po = vo2poMapping.apply(vo);
		List<OrderItemApportionment> apportionments = extractApportionment(vo, po, order.getId());
		po.setApportionmentInfo(apportionments);
		po.setOrder(order);
		return po;
	}


	Function<OmsOrderItem, OmsItemVo> po2voMapping = (item)-> {
		OmsItemVo vo = Copier.from(item).to(new OmsItemVo());
		vo.setCode(item.getProductCode());
		vo.setBrand(item.getBrandCode());
		vo.setCategory(item.getCategoryCode());
		vo.setName(item.getProductName());
		return vo;
	};


	Function<OmsItemVo, OmsOrderItem> vo2poMapping = (vo)-> {
		OmsOrderItem po = Copier.from(vo).to(new OmsOrderItem());
		po.setId(idService.getNextId());
		po.setProductCode(vo.getCode());
		po.setProductName(vo.getName());
		po.setCategoryCode(vo.getCategory());
		po.setBrandCode(vo.getBrand());
		if(StringUtils.isBlank(vo.getItemCode())){
			po.setItemCode(vo.getItemCode());
		}else{
			po.setItemCode(po.getId()+"");
		}
		return po;
	};


	private List<OrderItemApportionment> extractApportionment(OmsItemVo vo, OmsOrderItem item, Long orderId){
		item.setCouponOriginData(JsonUtils.obj2Json(vo.getCoupons()));
		item.setPromotionOriginData(JsonUtils.obj2Json(vo.getPromotions()));
		return Stream.concat(
				vo.getPromotions().stream().map(app->{
					OrderItemApportionment oia = new OrderItemApportionment();
					oia.setMode("promotion");
					oia.setItem(item);
					oia.setOrderId(orderId);
					oia.setBehavior(app.getBehavior());
					oia.setAmount(app.getAmount());
					oia.setId(idService.getNextId());
					oia.setName(app.getName());
					return oia;
				}),
				vo.getCoupons().stream().map(app->{
					OrderItemApportionment oia = new OrderItemApportionment();
					oia.setMode("coupon");
					oia.setItem(item);
					oia.setOrderId(orderId);
					oia.setId(idService.getNextId());
					oia.setBehavior(PrivilegeBehavior.discount);
					oia.setName(app.getName());
					oia.setType(app.getCouponType());
					oia.setAmount(app.getAmount());
					return oia;
				})
		).collect(Collectors.toList());
	}


}
